package com.btb.adapter.controller;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.web3j.utils.Convert;
import org.web3j.utils.Numeric;

import com.alibaba.fastjson.JSON;
import com.btb.adapter.model.UpChainFish;
import com.btb.adapter.service.OffChainService;
import com.btb.adapter.service.TransferRecordService;
import com.btb.adapter.service.UpChainApiService;
import com.btb.adapter.utils.ResponseText;
import com.btb.adapter.utils.Web3jUtils;
import com.btb.adapter.vo.request.FishUpChain;
import com.btb.adapter.vo.request.HeroUpChain;
import com.btb.adapter.vo.request.OffChainRequest;
import com.btb.adapter.vo.request.PayRequest;
import com.btb.adapter.vo.request.QueryPage;
import com.btb.adapter.vo.request.UpChainPageResult;
import com.btb.adapter.vo.request.UpChainQuery;
import com.btb.adapter.vo.request.UpChainResult;
import com.github.pagehelper.Page;
import com.google.common.collect.Maps;

@RequestMapping("/api")
@RestController
public class ChainController {
	@Autowired
	private TransferRecordService transferRecordService;
	@Autowired
	private UpChainApiService upChainService;
	@Autowired
	private OffChainService offChainService;

	@PostMapping("/heroUpChain")
	public ResponseText<Map<String, Object>> heroUpChain(@RequestBody HeroUpChain heros) {
		Map<String, Object> map = new HashMap<String, Object>();
		Integer size = Optional.ofNullable(heros).map(HeroUpChain::getList).map(x -> x.size()).orElse(0);
		String requestId = upChainService.heroUpChain(heros);
		map.put("requestId", requestId);
		map.put("size", size);
		return ResponseText.<Map<String, Object>>buildSuccess(map);
	}

	@PostMapping("/fishUpChain")
	public ResponseText<Map<String, Object>> fishoUpChain(@RequestBody FishUpChain fishs) {
		Map<String, Object> map = new HashMap<String, Object>();
		Integer size = Optional.ofNullable(fishs).map(FishUpChain::getList).map(x -> x.size()).orElse(0);
		String requestId = upChainService.fishoUpChain(fishs);
		map.put("requestId", requestId);
		map.put("size", size);
		return ResponseText.<Map<String, Object>>buildSuccess(map);
	}

	@PostMapping("/offChain")
	public ResponseText<Map<String, Object>> offChain(@RequestBody OffChainRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Integer size = Optional.ofNullable(request).map(OffChainRequest::getList).map(x -> x.size()).orElse(0);
		String requestId = offChainService.insert(request);
		map.put("requestId", requestId);
		map.put("size", size);
		return ResponseText.<Map<String, Object>>buildSuccess(map);
	}

	/**
	 * 最多20条数据
	 *
	 * @param type
	 * @param requestId
	 * @return
	 */
	@GetMapping("/query/{type}/{requestId}")
	public ResponseText<UpChainResult> queryUpChainResultWithRequestId(@PathVariable("type") final String type,
			@PathVariable("requestId") final String requestId) {
		UpChainResult build = upChainService.queryWithRequestId(type, requestId);
		return ResponseText.<UpChainResult>buildSuccess(build);
	}

	/**
	 * 可能有很多条数据
	 *
	 * @param type
	 * @param address
	 * @return
	 */
	@GetMapping("/page/{type}/{address}/{pageNo}")
	public ResponseText<QueryPage<UpChainPageResult>> queryUpChainResultWithAddress(
			@PathVariable("type") final String type, @PathVariable("address") final String address,
			@PathVariable("pageNo") final Long pageNo) {
		QueryPage<UpChainPageResult> queryUpChainResultWithAddress = upChainService.queryUpChainResultWithAddress(type,
				address, pageNo);
		return ResponseText.<QueryPage<UpChainPageResult>>buildSuccess(queryUpChainResultWithAddress);
	}

	/**
	 * 查询支付结果
	 *
	 * @param type
	 * @param address
	 * @return
	 */
	@GetMapping("/pay/{transactionHash}")
	public ResponseText<PayRequest> queryPayResult(@PathVariable("transactionHash") final String transactionHash) {
		PayRequest queryPayResult = transferRecordService.queryPayResult(transactionHash);
		return ResponseText.<PayRequest>buildSuccess(queryPayResult);
	}
}
